<?php

require_once dirname(__FILE__) . '/SpaceMaker.php';

class Board_Monopoly
{
    private $_spaces = null;

    public function __construct($space_maker)
    {
        $spaces = $space_maker->make('Go');
        $spaces->next($space_maker->make('Mediterranean Avenue'))
               ->next($space_maker->make('Community Chest'))
               ->next($space_maker->make('Baltic Avenue'))
               ->next($space_maker->make('Income Tax'))
               ->next($space_maker->make('Reading Railroad'))
               ->next($space_maker->make('Oriental Avenue'))
               ->next($space_maker->make('Chance'))
               ->next($space_maker->make('Vermont Avenue'))
               ->next($space_maker->make('Connecticut Avenue'))
               ->next($space_maker->make('Jail'))
               ->next($space_maker->make('St. Charles Place'))
               ->next($space_maker->make('Electric Company'))
               ->next($space_maker->make('States Avenue'))
               ->next($space_maker->make('Virginia Avenue'))
               ->next($space_maker->make('Pennsylvania Railroad'))
               ->next($space_maker->make('St. James Place'))
               ->next($space_maker->make('Community Chest'))
               ->next($space_maker->make('Tennessee Avenue'))
               ->next($space_maker->make('New York Avenue'))
               ->next($space_maker->make('Free Parking'))
               ->next($space_maker->make('Kentucky Avenue'))
               ->next($space_maker->make('Chance'))
               ->next($space_maker->make('Indiana Avenue'))
               ->next($space_maker->make('Illinois Avenue'))
               ->next($space_maker->make('B&O Railroad'))
               ->next($space_maker->make('Atlantic Avenue'))
               ->next($space_maker->make('Ventnor Avenue'))
               ->next($space_maker->make('Water Works'))
               ->next($space_maker->make('Marvin Gardens'))
               ->next($space_maker->make('Go To Jail'))
               ->next($space_maker->make('Pacific Avenue'))
               ->next($space_maker->make('North Carolina Avenue'))
               ->next($space_maker->make('Community Chest'))
               ->next($space_maker->make('Pennsylvania Avenue'))
               ->next($space_maker->make('Short Line'))
               ->next($space_maker->make('Chance'))
               ->next($space_maker->make('Park Place'))
               ->next($space_maker->make('Luxury Tax'))
               ->next($space_maker->make('Boardwalk'))
               ->next($spaces);

        $this->setSpaces($spaces);
    }

    public function setSpaces($spaces)
    {
        $this->_spaces = $spaces;
    }

    public function getSpaces()
    {
        return $this->_spaces;
    }

    public function getSpace($name)
    {
        $spaces = $this->getSpaces();

        $start_name = $spaces->getName();

        while (true) {
            if ($name == $spaces->getName()) {
                return $spaces;
            }

            $spaces = $spaces->next();

            if ($spaces->getName() == $start_name) {
                return false;
            }
        } 
    }
}
